package com.labreeze.spring.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.labreeze.spring.pojo.User;

@Repository("userDao")
public class UserDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	

	public User query(int id){
		String sql ="select * from tt_user where id=?";
		final List<User> list=new ArrayList<User>();
		jdbcTemplate.query(sql,new Object[]{id},  new RowCallbackHandler() {
			
			public void processRow(ResultSet rs) throws SQLException {
				// TODO Auto-generated method stub
				User user=new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				list.add(user);
			}
		});
		
		return list.get(0);
		
	}
	
	public List<User> query2(){
		String sql ="select * from tt_user";
		final List<User> list=new ArrayList<User>();
		jdbcTemplate.query(sql,  new RowCallbackHandler() {
			
			public void processRow(ResultSet rs) throws SQLException {
				// TODO Auto-generated method stub
				User user=new User();
				System.out.println("id:"+rs.getInt("id"));
				System.out.println("name:"+rs.getString("name"));
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				list.add(user);
			}
		});
		
		return list;
		
	}
	

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	
	
}
